// 状态接口
interface State {
    handle(context: Context): void;
}

// 灯关闭状态
class OffState implements State {
    handle(context: Context) {
        console.log('关灯');
        context.setState(new OnState());
    }
}

// 灯打开状态
class OnState implements State {
    handle(context: Context) {
        console.log('开灯');
        context.setState(new OffState());
    }
}

// 灯对象
class Context {
    private state!: State;
    constructor() {
        this.state = new OffState();
    }
    setState(state: State) {
        this.state = state;
    }
    request() {
        this.state.handle(this);
    }
}

// 使用
const context = new Context();
context.request(); // 开灯
context.request(); // 关灯